package com.cuitrip.util.time;

import android.content.Context;
import android.text.TextUtils;

import com.cuitrip.service.R;
import com.cuitrip.util.I118Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;

/**
 * TimeUtil用于格式化时间，接受<code>long</code>和<code>java.lang.String</code>两种类型输入
 * 考虑到输入和输出的时间格式的不确定性，采用的思路为先将inputTime转化成时间戳，
 * 兼顾地区和时区上的考虑，然后在根据outputTimeFormat进行格式上的转化，调用{@link TimeUtil#format(long, String, Locale, TimeZone)}  }
 * Created by ak
 */
public class TimeUtil {

    public static String formatTime(InputTime input, OutputTime output) {
        long time = input.getTime();
        return output.format(time);
    }

    public static final String DATE_FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";

    public static String formatDay_YM(Context context, long timeInMillis) {
        String s = context.getString(R.string.date_format2);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(s, I118Util.getLocale());
        return simpleDateFormat.format(timeInMillis);
    }

    public static String formatMonth(Context context, long time) {
        return formatDay(time + "", context.getString(R.string.date_format2));
    }

    public static String formatDay(Context context, long time) {
        return formatDay(time + "", context.getString(R.string.date_format));
    }

    public static String formatMinutes(Context context, String time) {
        return formatDay(time, context.getString(R.string.date_format_ymdhm));
    }

    public static String formatMinutes(Context context, String date, String inputFormat) {
        long time = parseString2Long(date, inputFormat);
        return formatMinutes(context, time + "");
    }

    /**
     * @param inputTime    此处inputtime为long形式的String
     * @param targetFormat
     * @return
     */
    public static String formatDay(String inputTime, String targetFormat) {
        long time;
        try {
            time = Long.parseLong(inputTime);
        } catch (NumberFormatException e) {
            time = parseString2Long(inputTime, DATE_FORMAT_SECOND);
        }
        return format(time, targetFormat, I118Util.getLocale(), I118Util.getTimeZone());
    }


    /*获取simpleDataFormat*/
    public static SimpleDateFormat getDateFormat(String outputFormat, Locale locale, TimeZone timeZone) {
        SimpleDateFormat dateFormat;
        if (locale != null) {
            dateFormat = new SimpleDateFormat(outputFormat, locale);
        } else {
            dateFormat = new SimpleDateFormat(outputFormat);
        }
        if (timeZone != null) {
            dateFormat.setTimeZone(timeZone);
        }
        return dateFormat;
    }

    public static final String format(String inputTime, String inputFormat, String outputFormat, Locale locale, TimeZone timeZone) {
        long longTime = parseString2Long(inputTime, inputFormat, locale, timeZone);
        return format(longTime, outputFormat, locale, timeZone);
    }


    public static long parseString2Long(String inputTime, String dateFormatSecond) {
        return parseString2Long(inputTime, dateFormatSecond, null, null);
    }

    public static long parseString2Long(String inputTime, String inputFormat, Locale locale, TimeZone timeZone) {
        if (TextUtils.isEmpty(inputTime)) {
            return -1;
        }
        //判断是否为long
        long time = parseString2Long(inputTime);
        if (time > 0) {
            return time;
        }
        SimpleDateFormat dateFormat = getDateFormat(inputFormat, locale, timeZone);
        try {
            return dateFormat.parse(inputTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1l;
    }

    public static long parseString2Long(String time) {
        try {
            long l = Long.parseLong(time);
            return l;
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    public static final String format(long inputTime, String outputFormat, Locale locale, TimeZone timeZone) {
        if (inputTime < 0) {
            return "";
        }
        SimpleDateFormat dateFormat = getDateFormat(outputFormat, locale, timeZone);
        return dateFormat.format(inputTime);
    }

    public static TimeZone getZeroZone() {
        return TimeZone.getTimeZone("GMT +000");
    }
}
